<?php
/**
 * Created by PhpStorm.
 * User: user
 * Date: 2017/3/13
 * Time: 20:52
 */
namespace haibao\jiepai\business;

class Picture extends Base
{
    /**
     * @var $_instance
     */
    private static $_instance;

    /**
     * instance()
     */
    private static function instance()
    {
        if (!self::$_instance) {
            self::$_instance = new \haibao\jiepai\data\Picture();
        }
        return self::$_instance;
    }

    public static function getPicture($pictureId)
    {
        return self::instance()->getPicture($pictureId);
    }

    public static function pushPraiseMsg($pictureId, $type, $fromUserId, $toUserId)
    {
        self::instance()->pushPraiseMsg($pictureId, $type, $fromUserId, $toUserId);
    }

    /**
     * 查询图片是否存在
     */
    public static function pictureExist($pictureId)
    {
        if (!$pictureId) {
            return false;
        }
        $picture = self::instance()->getPicture(intval($pictureId));
        return (isset($picture['Picture']) && $picture['Picture']) ? true : false;
    }

    /**
     * 根据图片id列表查询图片详细信息
     */
    public static function getPictureDetailInfo(array $pictureIdList)
    {
        $pictureIdList = array_unique(array_filter($pictureIdList));
        self::instance()->getPictureDetailInfo($pictureIdList);
    }


    /**
     * 查询用户是否收藏图片
     */
    public static function getIsCollect($userId, $imageIds)
    {
        $imageIds = array_unique(array_filter($imageIds));
        return self::instance()->getIsCollect($userId, $imageIds);
    }
    
    /**
     * 查询用户是否ContentID
     */
    public static function getContentIdByPictureIds($pictureIds = array())
    {
    	if(!is_array($pictureIds)){
    		$pictureIds = explode(',', $pictureIds);
    	}
    	return self::instance()->getContentIdByPictureIds($pictureIds);
    }

    /**
     * 为图片点赞或取消点赞
     */
    public static function praisePicture($userId, $pictureID, $type)
    {
        $userId = intval($userId);
        $pictureID = intval($pictureID);
        $type = intval($type);
        if ($type == 1) {
            self::instance()->insertUserAction($userId, $pictureID, 5);
        } elseif ($type == 0) {
            self::instance()->updateUserAction($userId, $pictureID, 5);
        }
    }

    /**
     * 查询用户是否点赞
     */
    public static function getIsPraise($userId, $imageIds)
    {
        $imageIds = array_unique(array_filter($imageIds));
        return self::instance()->getIsPraise($userId, $imageIds);
    }

    /**
     * 查询图片收藏来源文章或壁纸或instagram
     */
    public static function getCollectSource($imageIds)
    {
        $imageIds = array_unique(array_filter($imageIds));
        return self::instance()->getCollectSource($imageIds);
    }
    /**
     * 获取从私照收藏的图片对应的人物Id和文章Id
     * @param unknown $starIdArrResult
     * @param unknown $articleIdArrResult
     */
    public static function getCollectSourceById($imageIds){
        $imageIds = array_unique(array_filter($imageIds));
        return self::instance()->getCollectSourceById($imageIds);
    }
    
    /**
     * 查询图片评论内容
     */
    public static function getComments($userId, $pictureId, $commentId, $orderType = 1, $prevCursor = null, $nextCursor = null)
    {
        $comments = self::instance()->getComments($pictureId, $commentId, $orderType, $prevCursor, $nextCursor);
        if (isset($comments['results']) && $comments['results']) {
            $commentIds = array();
            foreach ($comments['results'] as $key => $comment) {
                array_push($commentIds, intval($comment['CommentID']));
            }
            // 获取评论回复
            $replyCommentArray = self::instance()->getReplyComment($commentIds);
           
            // 获取评论图片
            $picturesArray = self::instance()->getCommentPictures($commentIds, 1);
            // 用户是否点赞
            $isPraiseArray = self::instance()->getCommentUserActions($commentIds, $userId);

            if ($commentIds) {
                foreach ($comments['results'] as $key => &$comment) {
                    $comment['ReplyComments'] = isset($replyCommentArray[$comment['CommentID']]) ? $replyCommentArray[$comment['CommentID']] : array();
                    $comment['Pictures'] = isset($picturesArray[$comment['CommentID']]) ? $picturesArray[$comment['CommentID']] : array();
                    $comment['IsPraise'] = isset($isPraiseArray[$comment['CommentID']]) ? 1 : 0;
                }
            }

            $comments['results'] = self::instance()->hotCommentStatistic($comments['results'], $userId);

            $replyIds = array();
            foreach ($comments['results'] as $key => $value) {
                if (!$value['ReplyComments']) continue;
                foreach ($value['ReplyComments'] as $k => $reply) {
                    array_push($replyIds, $reply['ReplyID']);
                }
            }
            $replyIds = array_unique(array_filter($replyIds));
            // 获取回复是否点赞
            $replyPraiseArray = self::instance()->getCommentUserActions($replyIds, $userId);
            // 获取回复统计
            $statisticArray = array();
            if ($replyIds) {
                $statisticArray = self::instance()->getCommentReplyStatistic($replyIds);
            }

            foreach ($comments['results'] as $key => &$item) {
                $item['CommentID'] = intval($item['CommentID']);
                $item['Scores'] = intval($item['Scores']);
                $item['AuditStatus'] = intval($item['AuditStatus']);
                $item['IsPraise'] = intval($item['IsPraise']);
                foreach ($item['ReplyComments'] as $k => &$reply) {
                    $reply['ReplyID'] = intval($reply['ReplyID']);
                    $reply['AuditStatus'] = intval($reply['AuditStatus']);
                    $reply['IsPraise'] = isset($replyPraiseArray[$reply['ReplyID']]) ? 1 : 0;
                    $reply['Statistic']['PraiseNum'] = isset($statisticArray[$reply['ReplyID']]) ? $statisticArray[$reply['ReplyID']] : 0;
                    $reply['User']['UserID'] = intval($reply['User']['UserID']);
                    if ($item['AuditStatus']) {
                        $reply['Content'] = self::ReplaceSensitiveWords($reply['Content'], $reply['SensitiveWords']);
                    }
                    unset($reply['AuditStatus'], $reply['SensitiveWords']);
                   
                }
                $item['Statistic']['PraiseNum'] = intval($item['Statistic']['PraiseNum']);
                $item['Statistic']['ReplyNum'] = intval($item['Statistic']['ReplyNum']);

                if ($item['AuditStatus']) {
                    $item['Content'] = self::ReplaceSensitiveWords($item['Content'], $item['SensitiveWords']);
                }
                unset($item['AuditStatus'], $item['SensitiveWords']);
            }
        }
        return $comments;
    }

    /**
     * 根据评论ID查询图片
     * @param $commentId
     * @return bool|void
     */
    public static function getPictureByCommentID($commentId, $userId)
    {
        if(!$commentId){
            return false;
        }
        $picture = self::instance()->getPictureByCommentID($commentId);
        $pictureId = intval($picture['FileID']);
        $contentResult = \haibao\jiepai\business\Picture::getContentIdByPictureIds(array($pictureId));
        // 查询用户是否收藏图片和点赞数量
        $collectResult = \haibao\jiepai\business\Picture::getIsCollect($userId, array($pictureId));
        $praiseResult = \haibao\jiepai\business\Picture::getIsPraise($userId, array($pictureId));

        // 查询图片评论数量
        $commentResult = \haibao\jiepai\business\Comment::getCommentCountByImageIds(array($pictureId));

        // 查询图片收藏来源文章
        $sourceResult = \haibao\jiepai\business\Picture::getCollectSource(array($pictureId));

        //来源结果
        $sourceIdResult = \haibao\jiepai\business\Picture::getCollectSourceById(array($pictureId));
        
        $picture['IsPictureCollect'] = isset($collectResult[$pictureId]) ? intval($collectResult[$pictureId]) : 0;
        $picture['IsPicturePraise'] = isset($praiseResult[$pictureId]) ? intval($praiseResult[$pictureId]) : 0;
        $picture['CommentCount'] = isset($commentResult[$pictureId]) ? intval($commentResult[$pictureId]) : 0;
        $picture['CollectSource'] = isset($sourceResult[$pictureId]) ? $sourceResult[$pictureId] : '';
        $picture['SourceType'] = isset($sourceIdResult[$pictureId]) ? $sourceIdResult[$pictureId]['SourceType'] : 0;
        $picture['SourceTypeID'] = isset($sourceIdResult[$pictureId]) ? $sourceIdResult[$pictureId]['SourceTypeID'] : 0;
        $picture['FileID'] = $pictureId;
        $picture['ContentID'] = isset($contentResult[$pictureId])?$contentResult[$pictureId] : 0;
        return $picture;
    }

}